package cn.wlh.learn.webmagic.selenium;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.selenium.SeleniumDownloader;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * webMagic获取cookie登陆
 * https://blog.csdn.net/weixin_42244754/article/details/81540678
 * ，账户过期了，没法用了。爬出来了，但是没法用了。
 * Selenium
 */
public class DaZhongBJSelenium implements PageProcessor {
    private final static Log logger = LogFactory.get();

    private Site site = Site
            .me()
            .setRetryTimes(3)
            .setSleepTime(1000)
            .setTimeOut(10000)
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
            .addHeader("Connection", "keep-alive")
            .addHeader("X-Requested-With", "XMLHttpRequest")
            .addHeader("Content-Type",
                    "application/x-www-form-urlencoded;charset=utf-8")
            .addHeader(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
    // 用来存储cookie信息
    private Set<Cookie> cookies = new HashSet<>();

    @Override
    public void process(Page page) {

        // 抓取规则
        logger.warn("url=[{}]",page.getUrl());


        //列表页
        if (page.getUrl().regex(URL).match()) {
            Selectable xpath = page.getHtml().xpath("//div[@class=\"J_subject_thumb\"]");
            List<String> all1 = xpath.all();
            List<String> all = page.getHtml().xpath("//div[@class=\"J_subject_thumb\"]").links().regex(URL_POST).all();
            page.addTargetRequests(all);
            // 添加列表. //*[@id="list_left"]/div[3]/div[1]/div[2]/div
//            page.addTargetRequests(page.getHtml().links().regex(URL_LIST).all());
        //文章页
        } else {


            //*[@id="item_left"]/div[1]/div/div[2]/ul/li[1]/table/tbody/tr[3]/td[1]
            // 获得标题
            Html pageHtml = page.getHtml();
            logger.warn("详情页面内容=[{}]", pageHtml);

            String title = pageHtml.xpath("//*[@id=\"item_left\"]/div[1]/div/div[1]/h2").get();
            // 服务
//            List<String> fw_key = pageHtml.css("class", "key").all();
            List<String> fw_key1 = pageHtml.xpath("//*[@class=\"key\"]/text()").all();
//            List<String> fw_value = pageHtml.css("width", "*").all();
            List<String> fw_value1 = pageHtml.xpath("//*[@width=\"*\"]").all();
            // 图片
            List<String> imges = pageHtml.css("#roll > tbody > tr > td > a > img","src").all();
            //
        }
    }

    @Override
    public Site getSite() {
        StringBuilder sb = new StringBuilder();
        // 将获取到的cookie信息添加到webmagic中
        for (Cookie cookie : cookies) {
            String name = cookie.getName().toString();
            String value = cookie.getValue()
                    .toString();
            site.addCookie(name, value);
            sb.append(StrUtil.format("site.addCookie(\"{}\", \"{}\");\n",name,value));
        }
        logger.info("避免频繁登录,生成设置cookie的代码=[\n{}\n]",sb.toString());


        // add cookie:
        site.addCookie("GI9UnAb1_hash", "Mzk5NzkJN2FkYThiNTQ0ZmZkNWYwMWVlOGYyMjQ1MzlhNmUyNjg%3D");
        site.addCookie("_ga_H9SVQTP9N6", "GS1.1.1629794959.1.1.1629794965.0");
        site.addCookie("Hm_lpvt_53e8fd9c93dbf09d1e859d9cd5710b8e", "1629794965");
        site.addCookie("_ga_V2V1WM1EC7", "GS1.1.1629794959.1.1.1629794966.0");
        site.addCookie("TawkConnectionTime", "0");
        site.addCookie("_gat_gtag_UA_153613011_2", "1");
        site.addCookie("_ga", "GA1.2.2128670203.1629794962");
        site.addCookie("__tawkuuid", "e::dazhongbaojian5.com::ZeRJ1+sMx53FfdnrrKZN7iLRCpT07wTiXGb/FGezgFTmIo8cci0xZvHVZoO6DS/w::2");
        site.addCookie("Hm_lvt_53e8fd9c93dbf09d1e859d9cd5710b8e", "1629794962");
        site.addCookie("PHPSESSID", "fd8666ec5383fde9c8223a8082013da6");
        site.addCookie("_gid", "GA1.2.1021370485.1629794962");


        return site;
    }


    public final static String URL = "https://www.dazhongbaojian5.com/item-list-catid-5.html"; // 地址

//    public static final String URL_LIST = "http://blog\\.sina\\.com\\.cn/s/articlelist_1487828712_0_\\d+\\.html";

    // https://www.dazhongbaojian0.com/item-1647.html
//    public static final String URL_POST = "http://blog\\.sina\\.com\\.cn/s/blog_\\w+\\.html";
    public static final String URL_POST = "https://www.dazhongbaojian5.com/item-\\w+\\.html";

    public void login() {
        // 登陆
        System.setProperty("webdriver.chrome.driver",
                "D:/chromedriver.exe"); // 注册驱动
        WebDriver driver = new ChromeDriver();


        driver.get(URL);// 打开网址

        // 防止页面未能及时加载出来而设置一段时间延迟
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 设置用户名密码
        driver.findElement(By.id("main_username")).sendKeys("q262417"); // 用户名
        driver.findElement(By.id("main_password")).sendKeys("11904847a"); // 密码
        // 模拟点击
        driver.findElement(By.xpath("//*[@id=\"main_frm_login\"]/dl/dd[2]/button[1]"))
                .click(); // xpath语言：id为form-group-login的form下的button

        // 防止页面未能及时加载出来而设置一段时间延迟
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 获取cookie信息
        cookies = driver.manage().getCookies();
        //
        driver.close();
    }

    public static void main(String[] args) {
        logger.warn("1111111111");
        System.setProperty("selenuim_config","E:\\Projects\\webmagic-learn\\src\\main\\resources\\config.ini");
        DaZhongBJSelenium dome = new DaZhongBJSelenium();
//        dome.login(); // 登陆
        Spider.create(dome).addUrl(URL)
                .addPipeline(new JsonFilePipeline("D:\\webmagic\\"))
                .downloader(new SeleniumDownloader("D:/chromedriver.exe"))
                .thread(5).start();
    }
}